package com.gxa.admin.recycling.service.impl;

import com.gxa.admin.recycling.mapper.ChangePointsStandardMapper;
import com.gxa.admin.recycling.mapper.WasteMapper;
import com.gxa.admin.recycling.service.ChangePointsStandardService;
import com.gxa.admin.recycling.service.WasteService;
import com.gxa.admin.recycling.service.WasteTypeManagerService;
import com.gxa.admin.recycling.service.WasteTypeService;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("wasteTypeManagerService")
@Transactional(rollbackFor = Throwable.class)
public class WasteTypeManagerServiceImpl implements WasteTypeManagerService {


    @Autowired
    private WasteTypeService wasteTypeService;

    @Autowired
    private WasteService wasteService;

    @Autowired
    private ChangePointsStandardService changePointsStandardService;

    @Autowired
    WasteMapper wasteMapper;

    @Autowired
    ChangePointsStandardMapper changePointsStandardMapper;
    private List<Waste> wastes = null;

    private List<WasteType> wasteTypes = null;

    private List<ChangePointsStandard> changePointsStandards = null;


    @Override
    public List<WasteVo> findAll() {
        //先查询废品父类表

        //再通过废品的父类id查询到每个废品种类
        wastes = wasteService.selectAll();
        //再通过兑换详情表中查询到积分兑换标准
        wasteTypes = wasteTypeService.selectAll();
        changePointsStandards = changePointsStandardService.selectAll();
        List<WasteVo> list= new ArrayList<>();
        for (WasteType wasteType : wasteTypes) {

            for (Waste waste : wastes) {
                //判断上线
                WasteVo wasteVo = new WasteVo();

                    if (waste.getWasteTypeId().equals(wasteType.getId())) {
                        wasteVo.setWaste(waste);
                        wasteVo.setWasteTypeName(wasteType.getTypeName());
                        List<ChangePointsStandard> list2 = new ArrayList<>();
                        for (ChangePointsStandard changePointsStandard : changePointsStandards) {

                            if (waste.getId().equals(changePointsStandard.getWasteId())) {
                                list2.add(changePointsStandard);
                            }
                        }
                        wasteVo.setChangePointsStandardList(list2);
                        list.add(wasteVo);
                    }

            }

        }

        return list;
    }

    @Override
    public List<WasteType> findParentWasteType() {

        wasteTypes = wasteTypeService.selectAll();

        return wasteTypes;
    }

    @Override
    public void addWasteType(Waste waste,List<ChangePointsStandard> list) throws BusinessException {

        //添加物资类别
        wasteMapper.insertSelective(waste);

        Long id = waste.getId();

        if(list.get(0)==null){
            throw new BusinessException(ExceptionCode.NOTADD_ITEM);
        }
        //添加废品类别的兑换标准
        for (ChangePointsStandard changePointsStandard : list) {
            changePointsStandard.setWasteId(id);
            changePointsStandardMapper.insert(changePointsStandard);
        }

    }

    @Override
    public List<Waste> findWasteType(Long wasteTypeId) {
        Example example=new Example(Waste.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wasteTypeId",wasteTypeId);

        List<Waste> wastes = wasteMapper.selectByExample(example);

        return wastes;


    }

    @Override
    public void delete(Long wasteId) {
        //删除废品类
        Waste waste = wasteMapper.selectByPrimaryKey(wasteId);

        Long id = waste.getId();

        Example example=new Example(ChangePointsStandard.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("wasteId",wasteId);

        List<ChangePointsStandard> changePointsStandards = changePointsStandardMapper.selectByExample(example);

        for (ChangePointsStandard changePointsStandard : changePointsStandards) {
            //删除相应的兑换标准
            changePointsStandardMapper.deleteByPrimaryKey(changePointsStandard.getId());
        }

        wasteMapper.deleteByPrimaryKey(wasteId);

    }

    @Override
    public void updateState(Waste waste) {

        wasteMapper.updateByPrimaryKeySelective(waste);

    }
}
